<?php 
/**
 *  callServer API
 */
namespace Client\Common;

class CommonHelper
{ 
    
    static public function buildOrderNo(){
        return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }
    
    /**
     * 
     * @param unknown $url
     * @param unknown $data
     * @return string|mixed
     */
    public function httpRquest($url, $data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL, $url);
    
        if(!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
    
        $res = curl_exec($curl);
        if(curl_errno($curl)){
            return 'Errno'.curl_error($curl);
        }
        curl_close($curl);
    
        return $res;
    }
    
    
    /**
     * 
     * @param unknown $post_str
     * @return NULL|SimpleXMLElement
     */
    function turnMsgToObj($post_str){
        if(!empty($post_str))
        {
            $result = simplexml_load_string($post_str, 'SimpleXMLElement', LIBXML_NOCDATA);
        }else{
            $result = NULL;
        }
    
        return $result;
    }
    
    /*
     * 功能：取得给定日期所在周的开始日期和结束日期
     * 参数：$gdate 日期，默认为当天，格式：YYYY-MM-DD
     *      $first 一周以星期一还是星期天开始，0为星期天，1为星期一
     * 返回：数组array("开始日期", "结束日期");
     */
    function aweek($gdate = "", $first = 0){
        if (!$gdate){
            $gdate = date("Y-m-d");
        }
        $w = date("w", strtotime($gdate)); //取得一周的第几天,星期天开始0-6
        $dn = $w ? $w - $first : 6; //要减去的天数
        $st = date("Y-m-d", strtotime("$gdate -" . $dn . " days"));
        $en = date("Y-m-d", strtotime("$st +6 days"));
        return array($st, $en); //返回开始和结束日期
    }
    
    
    //获取客户端IP地址
    public function getClientIp(){
        $ip = $_SERVER['REMOTE_ADDR'];
        if (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP']))
        {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        }
        elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches))
        {
            foreach ($matches[0] AS $xip)
            {
                if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip))
                {
                    $ip = $xip;
                    break;
                }
            }
        }
        return $ip;
    }
    
    /**
     * SERVER_PORT Web 服务器使用的端口。默认值为 “80”。如果使用 SSL 安全连接，则这个值为用户设置的 HTTP 端口。
     * @return boolean
     */
    public function isHttps(){
        if(!empty($_SERVER['HTTPS']))  return 'https://';
        if($_SERVER['HTTPS'] === 1){  //Apache
            return 'https://';
        }elseif($_SERVER['HTTPS'] === 'on'){ //IIS
            return 'https://';
        }elseif($_SERVER['SERVER_PORT'] == 443){ //其他
            return 'https://';
        }
        return 'http://';
        
    }
    

    /**
     * 判断是否SSL协议
     * @return boolean
     */
    function is_ssl() {
        if(isset($_SERVER['HTTPS']) && ('1' == $_SERVER['HTTPS'] || 'on' == strtolower($_SERVER['HTTPS']))){
            return true;
        }elseif(isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['SERVER_PORT'] )) {
            return true;
        }
        return false;
    }

 

}